package net.libra.scrollui
{
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 *有滚动条的list
	 *@author elvisjiang
	 *@createTime 2011-7-15
	 * @param itemRenderer
	 * @param itemWidth
	 * @param itemHeight
	 * @param verticalGap
	 * @param maxHeight
	 * @param vslider
	 * @param vscrollBar
	 */
	public class ScrollList extends MyList
	{
		protected var _vslider:VSlider;
		protected var _vscrollBar:VScollBar;
		
		private var _scrollPower:Number = 10;//滑动鼠标滚轮时，控件滚动速度倍率。
		private var _altitudeorbit:Number;//轨道高
		private var _altitudeorbitRange:Number;//滚动范围
		private var _buttonHeight:Number;//按钮高度
		private var _oribt:Number = 1;//比率系数
		private var _pageSize:Number;//最大显示高度
		public var initialized:Boolean;
		
		public function ScrollList(itemRenderer:Class,itemWidth:Number,itemHeight:Number,verticalGap:Number,pageSize:Number,vslider:VSlider,vscrollBar:VScollBar)
		{
			this._vslider = vslider;
			this._vscrollBar = vscrollBar;
			this._pageSize = pageSize;
			super(itemRenderer,itemWidth,itemHeight,verticalGap);
		}
		override protected function init():void
		{
			mouseChildren = true;
			this.scrollRect = new Rectangle(0,0,this.itemWidth,this.pageSize);
			_vslider.button.y = 0;
			this.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelHandler);
			this.addEventListener(ListEvent.SOURCE_RENDER,updateBtns);
			_vslider.addEventListener(ListEvent.SCROLL,scroll);
			_vscrollBar.addEventListener(ListEvent.SCROLL,scroll);
		}
		override public function destroy():void
		{
			this.removeEventListener(ListEvent.SOURCE_RENDER,updateBtns);
			this.removeEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelHandler);
			_vslider.removeEventListener(ListEvent.SCROLL,scroll);
			_vscrollBar.removeEventListener(ListEvent.SCROLL,scroll);
			this._vslider = null;
			this._vscrollBar = null;
			super.destroy();
		}
		private function mouseWheelHandler(evt:MouseEvent):void
		{
			if(contentHeight > pageSize){
				if(evt.delta < 0){//向下滚动
					_vscrollBar.button_down();
				}else{//向上滚动
					_vscrollBar.button_up();
				}
			}else{
				if(evt.delta >= 0){//向下滚动
					if(scrollPosition > 0){
						_vscrollBar.button_up();
					}
				}
			}
		}
		protected var scrollPosition:Number = 0;//已经滑过的内容高度
		/**
		 * 
		 */ 
		private function scroll(evt:ListEvent):void
		{
			var posY:Number = evt.obj as Number;
//			trace("----------------------------------------------");
			
			scrollPosition = Math.round(posY * proportion);
//			trace("posY:",posY,"scrollPosition:",scrollPosition);
			this.scrollRect = new Rectangle(0,scrollPosition,this.itemWidth,this.pageSize);
		}
		private function updateBtns(evt:ListEvent):void
		{
			if(!initialized){
				dispatchEvent(new ListEvent(ListEvent.FIRST_RENDER,true));
				initialized = true;
			}
			updateContent();
			setButtonHeight();
			updateButtonPosition();
		}
		public function updateContent():void
		{
//			trace("scrollPosition:",scrollPosition);
			if(contentHeight > pageSize){
				if(contentHeight - scrollPosition <= pageSize)
				{
					scrollPosition = (contentHeight - pageSize);
					this.scrollRect = new Rectangle(0,scrollPosition,this.itemWidth,this.pageSize);
				}
			}
		}
		/**
		 *button.height / trackheight = pageSize / contentHeight
		 *
		 *thumb.height / track.height = pageSize / （maxScrollPosition - minScrollPosition + pageSize ）
		 *
		 */
		public function setButtonHeight():void
		{
			if(pageSize > contentHeight){
				_vslider.button.visible = false;
				this.scrollRect = new Rectangle(0,0,this.itemWidth,this.pageSize);
			}else{
				_vslider.button.visible = true;
//				var buttonHeight:Number = maxHeight * _vslider.trackHeight/contentHeight;
				var buttonHeight:Number = trackHeight * (pageSize /contentHeight);
//				trace("buttonHeight:",buttonHeight);
				_vslider.buttonHeight = buttonHeight;
			}
		}
		/**
		 *已经滑过的height/内容高度 = button.y/(trackHeight - button.height) 
		 * 
		 *thumb.y = track.y+(track.height-thumb.height)*((scrollPosition-minScrollPosition)/(maxScrollPosition-minScrollPosition));
		 */		
		public function updateButtonPosition():void
		{
//			trace("---------------------------------------");
//			trace("_vslider.button.y:",_vslider.button.y);
//			scrollPosition = Math.round((_vslider.button.y) * (contentHeight - pageSize)/(trackHeight - _vslider.button.height));
//			if(contentHeight - lastContentHeight > 0)//展开
//			{
//				
//			}else{//收起
//				
//			}
			
//			_vslider.button.y = Math.round( (trackHeight - _vslider.button.height)*scrollPosition / contentHeight);
			_vslider.button.y = Math.round(contentProportion*scrollPosition);
//			lastContentHeight = contentHeight;
//			trace("_vslider.button.y:",_vslider.button.y,"scrollPosition:",scrollPosition);
//			trace("scrollPosition:",scrollPosition);
//			trace("contentHeight:",contentHeight);
//			trace("_vslider.trackHeight:",_vslider.trackHeight);
//			trace("_vslider.button.height:",_vslider.button.height);
		}
//		protected var lastContentHeight:Number = 0;
		////////////////////////////////////////////////////
		
		//一像素移动多少content
		public function get proportion():Number
		{
			return (contentHeight - pageSize)/(trackHeight - _vslider.button.height);
		}
		//一个content多少像素
		public function get contentProportion():Number
		{
			return (trackHeight - _vslider.button.height)/(contentHeight - pageSize);
		}
		public function get pageSize():Number
		{
			return _pageSize;
		}
		public function get trackHeight():Number
		{
			return _vslider.trackHeight;
		}
	}
}